package UI;

/*
 * ispMainUI.java
 *
 * Created on September 22, 2008, 5:40 PM
 */
import Parser.Parser;
import Graph.SourceView;
import Graph.DrawGraph;
import Data.Interleavings;
import Data.GlobalStructure;

import javax.swing.JDialog;
import javax.swing.UIManager;
import javax.swing.JOptionPane;
import javax.swing.JButton;


import java.awt.Color;
import java.io.File;

import org.apache.log4j.Logger;
import org.apache.log4j.FileAppender;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PatternLayout;

/**
 *
 * @author  Sriram
 */
public class ispMainUI extends javax.swing.JFrame {

    static JDialog dialog;

    /** Creates new form ispMainUI */
    public ispMainUI() {
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        initComponents();       // automatic generated code for initializing UI        
    }
    
@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu_viewOptions = new javax.swing.JPopupMenu();
        jMenuItem_viewIntraCB = new javax.swing.JMenuItem();
        jFileChooser = new javax.swing.JFileChooser();
        jDialog_dispIntraCB = new javax.swing.JDialog();
        jFrame_dispIntraCB = new javax.swing.JFrame();
        jScrollPane_dispIntraCB = new javax.swing.JScrollPane();
        jFrame_SourceCodeView = new javax.swing.JFrame();
        jScrollPane1 = new javax.swing.JScrollPane();
        jEditorPane_SourceView = new javax.swing.JEditorPane();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenu2 = new javax.swing.JMenu();
        buttonGroup_srcView = new javax.swing.ButtonGroup();
        jPanel_Main = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel_deadlockResult = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane_displayPane = new javax.swing.JScrollPane();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jCheckBox_ShowMatch = new javax.swing.JCheckBox();
        jButton3 = new javax.swing.JButton();
        jLabel_currInterleaving = new javax.swing.JLabel();
        jButton_Goto = new javax.swing.JButton();
        jText_GotoInterleaving = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jRadioButton_showSource = new javax.swing.JRadioButton();
        jRadioButton_hideSource = new javax.swing.JRadioButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        jMenuItem_viewIntraCB.setText("View IntraCB");
        jMenuItem_viewIntraCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_viewIntraCBActionPerformed(evt);
            }
        });
        jPopupMenu_viewOptions.add(jMenuItem_viewIntraCB);

        javax.swing.GroupLayout jDialog_dispIntraCBLayout = new javax.swing.GroupLayout(jDialog_dispIntraCB.getContentPane());
        jDialog_dispIntraCB.getContentPane().setLayout(jDialog_dispIntraCBLayout);
        jDialog_dispIntraCBLayout.setHorizontalGroup(
            jDialog_dispIntraCBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 450, Short.MAX_VALUE)
        );
        jDialog_dispIntraCBLayout.setVerticalGroup(
            jDialog_dispIntraCBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 313, Short.MAX_VALUE)
        );

        jDialog_dispIntraCB.setSize(400, 400);

        jFrame_dispIntraCB.setTitle("IntraCB");

        javax.swing.GroupLayout jFrame_dispIntraCBLayout = new javax.swing.GroupLayout(jFrame_dispIntraCB.getContentPane());
        jFrame_dispIntraCB.getContentPane().setLayout(jFrame_dispIntraCBLayout);
        jFrame_dispIntraCBLayout.setHorizontalGroup(
            jFrame_dispIntraCBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jFrame_dispIntraCBLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane_dispIntraCB, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
                .addContainerGap())
        );
        jFrame_dispIntraCBLayout.setVerticalGroup(
            jFrame_dispIntraCBLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jFrame_dispIntraCBLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane_dispIntraCB, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
                .addContainerGap())
        );

        jEditorPane_SourceView.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        jEditorPane_SourceView.setFont(new java.awt.Font("Lucida Console", 0, 12));
        jScrollPane1.setViewportView(jEditorPane_SourceView);

        javax.swing.GroupLayout jFrame_SourceCodeViewLayout = new javax.swing.GroupLayout(jFrame_SourceCodeView.getContentPane());
        jFrame_SourceCodeView.getContentPane().setLayout(jFrame_SourceCodeViewLayout);
        jFrame_SourceCodeViewLayout.setHorizontalGroup(
            jFrame_SourceCodeViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jFrame_SourceCodeViewLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 608, Short.MAX_VALUE)
                .addContainerGap())
        );
        jFrame_SourceCodeViewLayout.setVerticalGroup(
            jFrame_SourceCodeViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jFrame_SourceCodeViewLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                .addContainerGap())
        );

        jMenu2.setText("Menu");
        jPopupMenu1.add(jMenu2);

        buttonGroup_srcView.add(jRadioButton_showSource);
        buttonGroup_srcView.add(jRadioButton_hideSource);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ISP");
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.LINE_AXIS));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setText("Showing Interleaving:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setText("Deadlock:");

        jLabel_deadlockResult.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel_deadlockResult.setText("Y/N");

        jPanel1.setAutoscrolls(true);

        jScrollPane_displayPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane_displayPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane_displayPane.setViewportBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        jScrollPane_displayPane.setAutoscrolls(true);
        jScrollPane_displayPane.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane_displayPane, javax.swing.GroupLayout.DEFAULT_SIZE, 878, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane_displayPane, javax.swing.GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
                .addContainerGap())
        );

        jButton1.setName("viewNextInterleaving"); // NOI18N
        jButton1.setText(">");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("<");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jCheckBox_ShowMatch.setFont(new java.awt.Font("Tahoma", 0, 14));
        jCheckBox_ShowMatch.setSelected(true);
        jCheckBox_ShowMatch.setText("Show Matches");
        jCheckBox_ShowMatch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox_ShowMatchActionPerformed(evt);
            }
        });

        jButton3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jButton3.setText("Clear");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel_currInterleaving.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel_currInterleaving.setText("1");

        jButton_Goto.setFont(new java.awt.Font("Tahoma", 0, 14));
        jButton_Goto.setText("Goto");
        jButton_Goto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_GotoActionPerformed(evt);
            }
        });

        jText_GotoInterleaving.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jText_GotoInterleavingActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setText("Switch Interleavings");

        jRadioButton_showSource.setFont(new java.awt.Font("Tahoma", 0, 14));
        jRadioButton_showSource.setText("Show Source");
        jRadioButton_showSource.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton_showSourceActionPerformed(evt);
            }
        });

        jRadioButton_hideSource.setFont(new java.awt.Font("Tahoma", 0, 14));
        jRadioButton_hideSource.setText("Hide Source");
        jRadioButton_hideSource.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton_hideSourceActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel_MainLayout = new javax.swing.GroupLayout(jPanel_Main);
        jPanel_Main.setLayout(jPanel_MainLayout);
        jPanel_MainLayout.setHorizontalGroup(
            jPanel_MainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_MainLayout.createSequentialGroup()
                .addGroup(jPanel_MainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel_MainLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel_MainLayout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel_currInterleaving, javax.swing.GroupLayout.DEFAULT_SIZE, 8, Short.MAX_VALUE)
                        .addGap(45, 45, 45)
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton_Goto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jText_GotoInterleaving, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                .addGap(30, 30, 30)
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel_deadlockResult, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jCheckBox_ShowMatch, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(jPanel_MainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton_hideSource, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
                    .addGroup(jPanel_MainLayout.createSequentialGroup()
                        .addComponent(jRadioButton_showSource, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(49, 49, 49)))
                .addContainerGap())
            .addGroup(jPanel_MainLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel_MainLayout.setVerticalGroup(
            jPanel_MainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_MainLayout.createSequentialGroup()
                .addGroup(jPanel_MainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel_MainLayout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(jRadioButton_showSource, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel_MainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel_MainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton_Goto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel_deadlockResult, javax.swing.GroupLayout.DEFAULT_SIZE, 19, Short.MAX_VALUE)
                                .addComponent(jLabel2)
                                .addComponent(jText_GotoInterleaving, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jCheckBox_ShowMatch, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jRadioButton_hideSource))
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel_MainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1)
                                .addComponent(jLabel_currInterleaving, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel_MainLayout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(jLabel3)))
                .addGap(11, 11, 11)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        getContentPane().add(jPanel_Main);

        jMenu1.setText("File");
        jMenu1.setFont(new java.awt.Font("Segoe UI", 0, 14));
        jMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        jMenuItem1.setFont(new java.awt.Font("Segoe UI", 0, 14));
        jMenuItem1.setText("Open Trace");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setFont(new java.awt.Font("Segoe UI", 0, 14));
        jMenuItem2.setText("Exit");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

// should delete
private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jMenu1ActionPerformed

// clears up the scroll pane
    void flushDisplay() {
        jLabel_currInterleaving.setText("");
        jLabel_deadlockResult.removeAll();
        if (dGraph != null) {
            jScrollPane_displayPane.remove(dGraph);
            dGraph.CleanUp();
        }
        jScrollPane_displayPane.getViewport().updateUI();
        this.jRadioButton_hideSource.setSelected(true);
//        jRadioButton_showSource.setSelected(false);
//        jRadioButton_hideSource.setSelected(false);
        this.repaint();
    }

    // call the paintGraph() routine 
    void fireUpGraph() {        
        if (GlobalStructure.getInstance().Exception == null) {
            try {
                if (srcView != null) {
                    srcView.cleanSrcView();
                    //jToggleButton1.setSelected(false);
                    //jToggleButton1.setText("Show Source");
                }
                setLabelDeadlock();     // sets Deadlock Y/N         
                GlobalStructure.getInstance().currInterleaving = 1;     // the very f irst interleaving will be displayed                            
                srcView = new SourceView();
                srcView.setUI(this);
                paintGraph();
                // set program order to true                
            } catch (Exception e) {
                System.out.print("firupGraph() triggered Exception : " + e.getMessage());
            }
        }
    }

    // File-> Open
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
// TODO add your handling code here:            
            int returnval = jFileChooser.showOpenDialog(null);
            logger.info("File -> Open Trace ");
            try {
                if (returnval == jFileChooser.APPROVE_OPTION) {
                    GlobalStructure.getInstance().flush();
                    File file = null;
                    flushDisplay();
                    file = jFileChooser.getSelectedFile();
                    logger.info("File " + "\'" + file.getName() + "\' Selected");
                    GlobalStructure.getInstance().CurrentDirectory = file.getParentFile();
                    new Parser().parse(file.toString());
                } else {
                    logger.info("File Selection Canceled");
                }
            } catch (Exception e) {
                System.out.println("File Chooser Exception : " + e.getMessage());
            }
            if (GlobalStructure.getInstance().Exception != null) {
                JOptionPane jerr = new JOptionPane();
                logger.error(GlobalStructure.getInstance().Exception);
                flushDisplay();
                jerr.showMessageDialog(this, GlobalStructure.getInstance().Exception);
                GlobalStructure.getInstance().flush();                
            } else 
                fireUpGraph();
}//GEN-LAST:event_jMenuItem1ActionPerformed

// '>' routine to step through the interleavings
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:   
            String logmsg = new String();
            logmsg += "Button \'>\' clicked : ";
            int currInterleaving = GlobalStructure.getInstance().currInterleaving;
            int nInterleavings = GlobalStructure.getInstance().nInterleavings;
            if (++currInterleaving <= nInterleavings) {
                GlobalStructure.getInstance().currInterleaving = currInterleaving;
//        jLabel_currInterleaving.setText(new Integer(currInterleaving).toString());
                paintGraph();
                jCheckBox_ShowMatch.setSelected(true);
                logmsg += "Showing Interleaving " + currInterleaving;
            } else {
                logmsg += "No Interleaving to show";
            }            
            
            // update the source
            if(srcView != null)
                srcView.writeSource();
            
            logger.info(logmsg);
}//GEN-LAST:event_jButton1ActionPerformed

// '<' routine to step through the interleavings
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
            String logmsg = new String();
            logmsg += "Button \'<\' clicked : ";
            int currInterleaving = GlobalStructure.getInstance().currInterleaving;
            if (--currInterleaving > 0) {
                GlobalStructure.getInstance().currInterleaving = currInterleaving;
                paintGraph();
                jCheckBox_ShowMatch.setSelected(true);
                logmsg += "Showing Interleaving " + currInterleaving;
            } else {
                logmsg += "No Interleaving to show";
            }
            
            // update source for new interleaving
            if(srcView != null)
                srcView.writeSource();
            logger.info(logmsg);
}//GEN-LAST:event_jButton2ActionPerformed

// Show/Hide Match Edge display
private void jCheckBox_ShowMatchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox_ShowMatchActionPerformed
// TODO add your handling code here:
            String logmsg = new String();
            logmsg = "Checkbox Clicked : ";
            if (!jCheckBox_ShowMatch.isSelected()) {
                dGraph.getGraphLayoutCache().remove(dGraph.MatchEdgeSet.toArray());
                dGraph.MatchEdgeSet.clear();
                dGraph.drawGraph();
                dGraph.repaint();                
                logmsg += " Hiding the Matches";
            } else {
                int iIndex = GlobalStructure.getInstance().currInterleaving - 1;
                Interleavings interLeaving = GlobalStructure.getInstance().interleavings.get(iIndex);
                dGraph.setCellLocation();
                dGraph.CreateEdges(interLeaving);
                dGraph.addMatchEdges();
                dGraph.setCellLocation();
                logmsg += " Showing the Matches";
            }
            //jRadioButton_showProgramOrder.setSelected(true);
            logger.info(logmsg);
}//GEN-LAST:event_jCheckBox_ShowMatchActionPerformed

// should delete
private void jMenuItem_viewIntraCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem_viewIntraCBActionPerformed
// TODO add your handling code here:
//    paintIntraCB();
}//GEN-LAST:event_jMenuItem_viewIntraCBActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
    dGraph.getGraphLayoutCache().remove(dGraph.Edges.toArray());
    dGraph.Edges.clear();
    dGraph.buildGraph();
    dGraph.repaint();
    logger.info("Clear button Clicked : Clearing the Edges");
}//GEN-LAST:event_jButton3ActionPerformed

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
// TODO add your handling code here:
    this.setVisible(false);
    logger.info("File -> Exit Clicked");
    this.dispose();
    System.exit(0);
}//GEN-LAST:event_jMenuItem2ActionPerformed

// should delete
private void jText_GotoInterleavingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jText_GotoInterleavingActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jText_GotoInterleavingActionPerformed

// jumps to a interleaving 
private void jButton_GotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_GotoActionPerformed
// TODO add your handling code here:
            int interleaving = 0;
            try {
                interleaving = Integer.parseInt(jText_GotoInterleaving.getText());
            } catch (NumberFormatException e) {
                System.out.println("Number invalid ");

            }

            if (interleaving > 0 && interleaving <= GlobalStructure.getInstance().nInterleavings) {
                GlobalStructure.getInstance().currInterleaving = interleaving;
                paintGraph();
            } else 
                new javax.swing.JOptionPane().showMessageDialog(this, "Invalid Interleaving Number");
            
            
            // update source
            if(srcView != null)
                srcView.writeSource();
}//GEN-LAST:event_jButton_GotoActionPerformed

private void jRadioButton_showSourceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_showSourceActionPerformed
// TODO add your handling code here:
    if(srcView != null) {
        if(srcView.window == null) {
            srcView.writeSource();
            srcView.showSource();
        }
        else
            srcView.showWindow();
        this.toFront();
        this.requestFocus();
    }
}//GEN-LAST:event_jRadioButton_showSourceActionPerformed

private void jRadioButton_hideSourceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_hideSourceActionPerformed
// TODO add your handling code here:
    if(srcView != null) {
        srcView.killWindow();
    }
}//GEN-LAST:event_jRadioButton_hideSourceActionPerformed


// delete

//delete
// delete

// delete

// delete
// sets the label value based on ENV variable
public void setLabelDeadlock()
{
    int inter = GlobalStructure.getInstance().nInterleavings;
    if (GlobalStructure.getInstance().deadlocked) 
    {
        jLabel_deadlockResult.setText("Yes");
        jLabel_deadlockResult.setForeground(Color.RED);
        //jLabel_deadlock_info.setText("Deadlock detected in Interleaving : " + inter);
    } 
    else 
    {
        jLabel_deadlockResult.setText("No");
        jLabel_deadlockResult.setForeground(Color.GREEN);
        //jLabel_deadlock_info.setText("");
    }    
}

// reads ENV var for current interleaving
// draws graph for the current interleaving
public void paintGraph()
{    
    int iIndex = GlobalStructure.getInstance().currInterleaving-1;
    int tInter = GlobalStructure.getInstance().nInterleavings;
    if(GlobalStructure.getInstance().interleavings.size() > 0) {
        Interleavings interLeaving = GlobalStructure.getInstance().interleavings.get(iIndex);       
        jLabel_currInterleaving.setText(new Integer((iIndex + 1)).toString() + "/" + new Integer(tInter).toString());   
        dGraph = new DrawGraph();
        dGraph.removeAll();
        dGraph.CreateCells(interLeaving);
        dGraph.CreateEdges(interLeaving);   
        dGraph.drawGraph();    
        dGraph.setAutoResizeGraph(true);    
        jScrollPane_displayPane.getViewport().add(dGraph); 
        jCheckBox_ShowMatch.setSelected(true);   
        if(srcView != null)
            dGraph.setSourceView(srcView);
        //jRadioButton_showProgramOrder.setSelected(true);
    }
    // ---------- displays intraCB
}

// initialize the log4j logger
public static void initLogger()
{
    logger = Logger.getLogger(ispMainUI.class);
    fileAppender = new FileAppender();
    layout = new PatternLayout();
    
    logger.setAdditivity(false);    
    
    fileAppender.setAppend(false);
    fileAppender.setFile("UIlog");
    fileAppender.activateOptions();   
    
    layout.setConversionPattern("%r [%t] %-5p %c - %m%n");
    fileAppender.setLayout(layout);
    logger.addAppender(fileAppender);
    BasicConfigurator.configure(fileAppender);
}

    /**
     * @param args the command line arguments
     */
    public static void main(final String args[]) {                 
        
        initLogger();
        try{
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());                
           }
          catch(Exception e)
          {
            System.out.println(e.getMessage());
          }    
        String logmsg = new String();
        if(args.length > 0) {
            logger.info("UI fired with argument " + args[0]);
             new Parser().parse(args[0]);                                    
        }
        else
                logger.info("UI fired without any argument");
                
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {                  
                ispMainUI UI = new ispMainUI();
                UI.setVisible(true);
                UI.toFront();
                if(args.length > 0) 
                {
                    File file = new File(args[0]);                        
                    UI.jFileChooser.setCurrentDirectory(file);
                    UI.fireUpGraph();   
                    GlobalStructure.getInstance().CurrentDirectory = file.getParentFile();
                }                                    
            }         
        });
    }
        
    public static Logger logger;                // log4j logger
    private static FileAppender fileAppender;   // logs messages onto a file
    private static PatternLayout layout;        // defines the layout for log messages
    DrawGraph dGraph;                           // 
    SourceView srcView;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup_srcView;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton_Goto;
    private javax.swing.JCheckBox jCheckBox_ShowMatch;
    private javax.swing.JDialog jDialog_dispIntraCB;
    private javax.swing.JEditorPane jEditorPane_SourceView;
    private javax.swing.JFileChooser jFileChooser;
    private javax.swing.JFrame jFrame_SourceCodeView;
    private javax.swing.JFrame jFrame_dispIntraCB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel_currInterleaving;
    private javax.swing.JLabel jLabel_deadlockResult;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem_viewIntraCB;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel_Main;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JPopupMenu jPopupMenu_viewOptions;
    public javax.swing.JRadioButton jRadioButton_hideSource;
    public javax.swing.JRadioButton jRadioButton_showSource;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane_dispIntraCB;
    private static javax.swing.JScrollPane jScrollPane_displayPane;
    private javax.swing.JTextField jText_GotoInterleaving;
    // End of variables declaration//GEN-END:variables
}
